@file:Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")

package com.sx.pipeline.ui.mine

import android.app.Application
import android.net.Uri
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.config.AppConstants
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.repos.http.res.ContactUserVo
import com.sx.pipeline.repos.http.res.UserInfoVO
import com.sx.pipeline.repos.http.res.VersionUpdateVO
import com.sx.pipeline.utils.CacheDataManager
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers
import io.rong.imkit.RongIM

/**
 * @Author: YY
 * @Date: 2022/3/7
 * @Description: TODO
 */
class MineVM(app: Application) : CYBaseViewModel(app) {

    override fun registerRxBus() {
        super.registerRxBus()
    }


    /**
     * 加载缓存数据
     */
    fun loadData() {
        val userInfoObservable = Observable.create<UserInfoVO?> {
            it.onNext(mModel.getCacheData(AppConstants.CacheKey.CACHE_USER_INFO_DATA) as UserInfoVO?)
        }.subscribeOn(Schedulers.io())
        addSubscribe(
            userInfoObservable
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribe({
                    userInfoDataEvent.postValue(it)
                }) {
                    it.printStackTrace()
                    updateUserInfo()
                }
        )
    }


    /**
     * 获取app版本信息
     */
    val appVersionEvent: SingleLiveEvent<VersionUpdateVO> = SingleLiveEvent()
    fun getAppVersion() {
        addSubscribe(
            mModel.getAppVersion()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<VersionUpdateVO, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: VersionUpdateVO) {
                        appVersionEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")
                    }
                })
        )
    }


    /**
     * 注销账号
     */
    fun cancelAccount() {
        addSubscribe(
            mModel.cancelAccount()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                       Logs.d("cancelAccount $result")
                        mModel.clearToken()
                        RouteManager.Login.intentToLogin(
                            getApplication<Application>().applicationContext, object : NavCallback() {
                            override fun onArrival(postcard: Postcard?) {
                                CacheDataManager.clearAllCache(getApplication<Application>().applicationContext)
                                //融云断开连接
                                RongIM.getInstance().logout()
                                ActivityUtils.finishAllActivities()
                            }
                        })
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")
                    }
                })
        )
    }

    var imId = SingleLiveEvent<Int>()
    fun intentService(){
        addSubscribe(mModel.getAppServiceId()
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<ContactUserVo,BaseViewModel<*>>(this,true){
                override fun onResult(result: ContactUserVo) {
                    dismissLoadingDialog()
                    val userInfo = io.rong.imlib.model.UserInfo(
                        result.userUnitId.toString(),
                        result.userName,
                        Uri.parse(result.avatar)
                    )
                    RongIM.getInstance().refreshUserInfoCache(userInfo)
                    imId.postValue(result.userUnitId)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                    dismissLoadingDialog()
                }

            })
        )
    }
}